/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fieo.sgee.logic;

import br.com.fieo.sgee.dao.DaoFactory;
import br.com.fieo.sgee.exception.ValidFieldException;
import br.com.fieo.sgee.model.Kwh;

/**
 *
 * @author Claudete
 */
public class KwhLogic {

    private static KwhLogic _intance = null;
    private final DaoFactory factory;

    static public KwhLogic getInstance() {
        if (_intance == null) {
            _intance = new KwhLogic();
        }
        return _intance;
    }

    private KwhLogic() {
        factory = new DaoFactory();
    }

    public Kwh loadLast() {
        return factory.getKwhDAO().loadLast();
    }

    public void merge(Kwh kwh) throws ValidFieldException {

        try {

            if (kwh.getValue() < 0) {
                throw new ValidFieldException(ValidFieldException.VALUE_INVALID_POSITIVE);
            }

            factory.beginTransaction();
            factory.getKwhDAO().merge(kwh);
            factory.commit();

        } catch (ValidFieldException e) {
            factory.rollback();
            throw e;
        }

    }
}
